home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_10_10 / 1010060a < prev    next >
Text File  |  1992-08-08  |  2KB  |  73 lines

  1. /*  FILE: mkcrc32.c
  2.     DATE: 910917:1632
  3.     LMOD: 910917:1706
  4.     FOR:  generate tables for CCITT crc32
  5.           polynomial is
  6.     x**32+x**26+23+22+16+12+11+10+8+7+5+4+2+1+0
  7.     (x** left out after second step for short)
  8. */
  9. #include <stdio.h>
  10.  
  11. long crc[256];
  12. long magic=
  13.     (1L<<26)+(1L<<23)+(1L<<22)+(1L<<16)+(1L<<12)+
  14.     (1L<<11)+(1L<<10)+(1L<<8)+(1L<<7)+(1L<<5)+
  15.     (1L<<4)+4+2+1;  /* the polynomial */
  16.  
  17. /* format string for output */
  18. char ofmt[]="\t  db\t0%02xh,0%02xh,0%02xh,0%02xh"
  19.         "\t; %02x-%02x\n";
  20.  
  21. long tblwd(long i)  /* calculate table entry */
  22. {
  23.     int c; long f;
  24.     for(i<<=24,c=0;c++<8;) {
  25.     f=i&0x80000000L; i<<=1;
  26.     if(f) i^=magic;
  27.     }
  28.     return(i);
  29. }
  30.  
  31. main()    /* output split CRC-32 tables in MASM form */
  32. {
  33.     int i;
  34.     for(i=0;i<256;++i) crc[i]=tblwd(i);
  35.     printf(
  36.     "; crc tables: CCITT CRC-32, polynomial %lx\n",
  37.     magic);
  38.     printf("\ncrct0");
  39.     for(i=0;i<256;i+=4)
  40.     printf(ofmt,
  41.         (int)((crc[i]>>24)&0xff),
  42.         (int)((crc[i+1]>>24)&0xff),
  43.         (int)((crc[i+2]>>24)&0xff),
  44.         (int)((crc[i+3]>>24)&0xff),
  45.         i,i+3);
  46.     printf("\ncrct1");
  47.     for(i=0;i<256;i+=4)
  48.     printf(ofmt,
  49.         (int)((crc[i]>>16)&0xff),
  50.         (int)((crc[i+1]>>16)&0xff),
  51.         (int)((crc[i+2]>>16)&0xff),
  52.         (int)((crc[i+3]>>16)&0xff),
  53.         i,i+3);
  54.     printf("\ncrct2");
  55.     for(i=0;i<256;i+=4)
  56.     printf(ofmt,
  57.         (int)((crc[i]>>8)&0xff),
  58.         (int)((crc[i+1]>>8)&0xff),
  59.         (int)((crc[i+2]>>8)&0xff),
  60.         (int)((crc[i+3]>>8)&0xff),
  61.         i,i+3);
  62.     printf("\ncrct3");
  63.     for(i=0;i<256;i+=4)
  64.     printf(ofmt,
  65.         (int)(crc[i]&0xff),
  66.         (int)(crc[i+1]&0xff),
  67.         (int)(crc[i+2]&0xff),
  68.         (int)(crc[i+3]&0xff),
  69.         i,i+3);
  70.     printf("\n");
  71.     exit(0);
  72. }
  73.